Obtain a bearer token from Itron Identity
Request
Example in cURL:
Copy
curl --location 'https://idenserver.itrontotal.com/connect/token/' \
--header 'ContentType: application/x-www-form-urlencoded' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'client_id={{ClientID}}' \
--data-urlencode 'client_secret={{ClientSecret}}' \
--data-urlencode 'scope=ThirdPartyGatewayApi' \
--data-urlencode 'grant_type=client_credentials'
Example in C#:
Copy
using System;
using System.ComponentModel.DataAnnotations;
using System.IdentityModel.Tokens.Jwt;
using System.Net.Http.Headers;
using System.Text.Json;
//This example application shows how you can use the Identity Server to get a token and then use that token to make a request to the API
namespace MyApp
{
public class Token
{
public string access_token { get; set; }
public JwtSecurityToken jwtSecurityToken { get; set; }
}
public class BatchToken
{
public string name { get; set; }
public string type { get; set; }
public string @default { get; set; }
}
public class BatchQueryResponse
{
public BatchToken[] tokens { get; set; }
}
internal class Program
{
private static readonly HttpClient client = new HttpClient();
private static Token token;
private static readonly Guid data_product_id = new Guid("2211D5AC-87D6-45DC-94CE-B82ADA1037DF"); //Provided by Itron
private static readonly Guid subscription_id = new Guid("2E5C5952-29D6-4D85-B25A-4A53226BB6FA"); //Provided by Itron
private static readonly string clientId = "[Provided by itron]";
private static readonly string clientSecret = "[Provided by itron]";
private static readonly string scope = "ThirdPartyGateway";
private static readonly string tokenUrl = "https://idenserver.itrontotal.com/connect/token";
private static readonly string base_api_url = ""https://servicestest2.itrontotaltest.com"";
//Caches a token and returns it if it is still valid, otherwise gets a new one
public static async Task<Token> GetTokenIfNecessary()
{
//If we don't have a token or it is expiring, get a new one
if (token == null)
{
token = await GetToken();
}
else if (token.jwtSecurityToken.ValidTo < DateTime.UtcNow.AddMinutes(-1))
{
token = await GetToken();
}
return token;
}
//Get a token from the Identity Server
private static async Task<Token> GetToken()
{
//These values should come from an outside configuration source and not be hard coded as this example is
var values = new Dictionary<string, string>
{
{ "client_id", clientId },
{ "client_secret", clientSecret },
{ "scope", scope },
{ "grant_type", "client_credentials" }
};
var content = new FormUrlEncodedContent(values);
var response = await client.PostAsync(tokenUrl, content);
var responseString = await response.Content.ReadAsStringAsync();
var token = JsonSerializer.Deserialize<Token>(responseString);
token.jwtSecurityToken = new JwtSecurityToken(token.access_token);
return token;
}
static void Main()
{
//Get a token
var token = GetTokenIfNecessary().Result;
//Make a request to the API
var request = new HttpRequestMessage(HttpMethod.Get, $"{base_api_url}/data-products/{data_product_id}/subscriptions/" +
$"{subscription_id}/batch-request-parameters");
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token.access_token);
var response = client.SendAsync(request).Result;
var responseString = response.Content.ReadAsStringAsync().Result;
Console.WriteLine(responseString);
BatchQueryResponse batchQueryResponse = JsonSerializer.Deserialize<BatchQueryResponse>(responseString);
Console.WriteLine(batchQueryResponse.tokens[0].name);
}
}
}
Parameters
Name |
Type |
Format |
Description |
---|---|---|---|
ClientID |
string |
UUID |
REQUIRED. A public identifier for applications. Provided by Itron. |
ClientSecret |
string |
UUID |
REQUIRED. A secret known only to the application and the authorization server. It is essentially the application's own password. Provided by Itron. |
ClientScope |
string |
none |
REQUIRED. An identifier for resources that a client wants to access. Set to 'ThirdPartyGatewayApi'. |